CellposeをM1 Mac GPUで動かす
#5_image_analysis
CellposeはデフォルトだとNVIDIAのGPUで動くようになっているが、PytorchがApple Siliconに対応したことで、M1 MacでもGPUでsegmentationや学習ができるようになった。多分M2, M3でもいける。--> 2024.10.31 M3 Macでも問題なく動く。
Winはこちら Cellpose test (win)
注意
2024.10.31 Cellposeの最新版Ver.3.1.0でMPSを使って計算しようとするとエラーが出る。3.0.10では問題ない。cudaでは未確認。Windowsではver.3.1.0でも大丈夫ぽい。
環境構築
Miniconda + conda-forgeによる環境構築 (2024.06.26)を参照
Pytorchとcellposeは入れておく。
Pytorchは、例えばconda install pytorch::pytorch torchvision torchaudio -c pytorch
Cellpose
学習済みのモデルでsegmentationを行うnotebookの例
code:jupyter notebook
########## Import libraries ##############
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from cellpose import models, io, plot, core
from cellpose.io import imread
import torch
code:jupyter notebook
############## Load images ##############
fname = 'test.tif' ## Grayscale, Stacked images
imgs = io.imread(fname)
for img in imgs:
plt.figure(figsize=(2,2))
plt.imshow(img)
plt.axis('off')
plt.show()
code:jupyter notebook
############## Cellpose segmentation by GPU ##############
%%time
device = core.assign_device(device='mps') ### これが大事
print(device)
model = models.Cellpose(gpu=True, model_type="cyto3",device=device0)
chan = 0,0
masks,flows = [],[]
for i,img in enumerate(imgs):
mask, flow, style, diam = model.eval(img, diameter=30,channels=chan)
masks.append(mask)
flows.append(flow)
io.save_to_png(img, mask, flow, 'mask/'+str(i)+'.png')
code:jupyter notebook
############## Cellpose segmentation by CPU ##############
%%time
model = models.Cellpose(gpu=False, model_type="cyto3")
chan = 0,0
masks,flows = [],[]
for i,img in enumerate(imgs):
mask, flow, style, diam = model.eval(img, diameter=30,channels=chan)
masks.append(mask)
flows.append(flow)
io.save_to_png(img, mask, flow, 'mask/'+str(i)+'.png')
code:jupyter notebook
############## Display the segmentaion results ##############
for img, mask,flow in zip(imgs,masks,flows):
fig = plt.figure(figsize=(12,5))
plot.show_segmentation(fig, img, mask, flow0, channels=chan)
plt.tight_layout()
plt.show()
created by y-goto.icon
2024.06.26